package com.topisv.tms.service.returnGood;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import org.hibernate.ejb.criteria.predicate.ComparisonPredicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.ReturnGood;
import com.topisv.tms.entity.stock.instorage.pre.InstorageBillPre;
import com.topisv.tms.entity.work.WorkTask;
import com.topisv.tms.repository.ReturnGoodDao;
import com.topisv.tms.repository.stock.preInstorage.InstorageBillPreDao;
import com.topisv.tms.repository.work.WorkOrderDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly=true)
public class returnGoodService extends BaseService{
	
	@Autowired
	private ReturnGoodDao returnGoodDao;
	@Autowired
	private InstorageBillPreDao instorageBillPreDao;
	@Autowired
	private WorkOrderDao workOrderDao;
	
	/**
	 * 查所有
	 * @return
	 */
	public List<ReturnGood> findReturnGoods(){
		return returnGoodDao.findReturnGood();
	}
	/**
	 * 保存/修改
	 * @param returnGood
	 * @return
	 */
	@Transactional(readOnly=false)
	public ReturnGood saveReturnGood(ReturnGood returnGood){
		return returnGoodDao.save(returnGood);
	}
	/**
	 * 根据id查找对象
	 * @param id
	 * @return
	 */
	public ReturnGood findRg(Long id){
		return returnGoodDao.findOne(id);
	}
	/**
	 * 分页
	 * @param paramHolder
	 * @return
	 */
	public Page<ReturnGood> getReturnGood(QueryParamHolder paramHolder) {
		Page<ReturnGood> returnGoods = findByJson(paramHolder, returnGoodDao, ReturnGood.class);
		return returnGoods;
	}
	/**
	 * 查询能否匹配
	 * @param returnexpressno	退货快递单号
	 * @param oldorderno	原订单号
	 *  @param oldexpressno 原快递单号
	 * @return
	 */
	public InstorageBillPre findReturnGoodByWhere(Map<String,Object> param){
		Specification<InstorageBillPre> spec = getWhereClause(param);
		InstorageBillPre billPre = this.instorageBillPreDao.findOne(spec);
		return billPre;
	}
	
	public String ReturnGood(String returnexpressno,String oldorderno){
		return returnGoodDao.findReturnGood(returnexpressno, oldorderno);
	}
	
	/**
	 * 查询条件封装
	 * @param param
	 * @return
	 */
	private Specification<InstorageBillPre> getWhereClause(final Map<String, Object> param) {
		return new Specification<InstorageBillPre>(){
			@Override
		    public Predicate toPredicate(Root<InstorageBillPre> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
				  Predicate pre = cb.conjunction();
				  //returnExpressno
			      String returnexpressno=(String) param.get("returnexpressno");
			      if(StringUtils.isNotEmpty(returnexpressno)){
						pre.getExpressions().add(cb.equal(r.get("expressNo"),returnexpressno));
				  }
			      //oldCusOrderNo
			      String oldCusOrderNo=(String) param.get("oldorderno");
			      if(StringUtils.isNotEmpty(oldCusOrderNo)){
			    	    Predicate[] oldOrderNoArray = new Predicate[2];
			    	    oldOrderNoArray[0] = cb.equal(r.get("oldCusOrderNo"), oldCusOrderNo);
			    	    oldOrderNoArray[1] = cb.equal(r.get("customerOrderNo"), oldCusOrderNo);
						pre.getExpressions().add(cb.or(oldOrderNoArray));
//						pre.getExpressions().add(cb.equal(r.get("oldCusOrderNo"),oldCusOrderNo));
				  }
			      //oldExpressno
			      String oldexpressno = (String) param.get("oldexpressno");
			      if(StringUtils.isNotEmpty(oldexpressno)){
			    	  String oldorderno = workOrderDao.findOldOrderNo(oldexpressno);
			    	  pre.getExpressions().add(cb.equal(r.get("oldCusOrderNo"),oldorderno));
			      }
			      
			      pre.getExpressions().add(cb.notEqual(r.get("orderStatus"),3));
			      return pre;
			}
		  };
	}
	
	@Transactional(readOnly=false)
	public void deleteReturnGood(Long id){
		ReturnGood returngood=returnGoodDao.findOne(id);
		returnGoodDao.delete(returngood);
	}
}
